﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BrightIdeasSoftware;
using MyDownloader.Core;

namespace Teddy
{
    public partial class FormMain : Form
    {
        List<TEDTalk> talks = null;
        DownloadManager manager = null;
        CheckState dlRunning = CheckState.Unchecked;

        public FormMain()
        {
            InitializeComponent();
            talks = GetTalksList();
            manager = new DownloadManager();

            this.toolStripButtonAbout.Click += toolStripButtonAbout_Click;
            this.toolStripButtonDownload.Click += toolStripButtonDownload_Click;
            this.toolStripButtonRebuild.Click += toolStripButtonRebuild_Click;

            this.fastObjectListViewTalks.SetObjects(talks);
            this.fastObjectListViewTalks.FormatRow += fastObjectListViewTalks_FormatRow;
            this.fastObjectListViewTalks.ItemChecked += fastObjectListViewTalks_ItemChecked;

            this.buttonSelAll.Click += buttonSelAll_Click;
            this.buttonSelAlternative.Click += buttonSelAlternative_Click;
            this.buttonSelDeAll.Click += buttonSelDeAll_Click;
            this.buttonSelFinished.Click += buttonSelFinished_Click;

            manager.DownloadEnded += manager_DownloadEnded;
        }

        void fastObjectListViewTalks_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            talks[e.Item.Index].Marked = e.Item.Checked;
            this.fastObjectListViewTalks.Refresh();
        }

        void toolStripButtonRebuild_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void toolStripButtonDownload_Click(object sender, EventArgs e)
        {
            if (dlRunning == CheckState.Checked)
            {
                manager.PauseAll();
                //dlRunning = false;
            }
            else if(dlRunning==CheckState.Indeterminate)
            {
                /*manager.Downloads
                dlRunning = true;*/
            }
        }

        void buttonSelFinished_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < talks.Count; i++)
            {
                if (talks[i].Downloaded)
                {
                    talks[i].Marked = true;
                }
            }
            this.fastObjectListViewTalks.Refresh();
        }

        void buttonSelDeAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < talks.Count; i++)
            {
                talks[i].Marked = false;
            }
            this.fastObjectListViewTalks.Refresh();
        }

        void buttonSelAlternative_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < talks.Count; i++)
            {
                talks[i].Marked = !(talks[i].Marked);
            }
            this.fastObjectListViewTalks.Refresh();
        }

        void buttonSelAll_Click(object sender, EventArgs e)
        {
            for(int i=0;i<talks.Count;i++)
            {
                talks[i].Marked = true;
            }
            this.fastObjectListViewTalks.Refresh();
        }

        void manager_DownloadEnded(object sender, DownloaderEventArgs e)
        {
            throw new NotImplementedException();
        }

        void fastObjectListViewTalks_FormatRow(object sender, FormatRowEventArgs e)
        {
            TEDTalk talk = (TEDTalk)e.Model;
            if (talk.Downloaded)
                e.Item.BackColor = Color.LightGreen;
        }

        void toolStripButtonAbout_Click(object sender, EventArgs e)
        {
            (new AboutBoxMain()).ShowDialog(this);
        }

        private List<TEDTalk> GetTalksList()
        {
            List<TEDTalk> talks = new List<TEDTalk>();
            talks.Add(new TEDTalk("Test", "Gaurav", false));
            talks.Add(new TEDTalk("Kya chopi milegi?", "Tedha", true));
            return talks;
        }

        private void SetDownloadingState(bool state)
        {
            this.buttonSelAll.Enabled = !state;
            this.buttonSelAlternative.Enabled = !state;
            this.buttonSelDeAll.Enabled = !state;
            this.buttonSelFinished.Enabled = !state;
            this.fastObjectListViewTalks.Enabled = !state;
            this.toolStripButtonAbout.Enabled = !state;
            this.toolStripButtonRebuild.Enabled = !state;
            this.toolStripButtonDownload.Text = ((state) ? "Pause" : "Download");
            this.numericUpDownSegments.Enabled = !state;
        }
    }
}
